﻿using System.Collections.Generic;
using Entitas;
/// <summary>
/// when a collision entity is created and its gets a target upon collision. the collision target takes damage.
/// </summary>
class CollisionDamageSystem : ReactiveSystem<CollisionEntity>
{
    EnemyContext enemyContext;
    PlayerContext playerContext;
    public CollisionDamageSystem(Contexts contexts) : base(contexts.collision)
    {
        enemyContext = contexts.enemy;
        playerContext = contexts.player;
    }

    protected override void Execute(List<CollisionEntity> entities)
    {
        foreach (var e in entities)
        {
            IHitPointEntity TargetEntity;
            switch (e.targetInformation.ContextName)
            {
                
                case "Player":
                   
                        TargetEntity = playerContext.GetEntityWithName(e.targetInformation.Name);
                        TargetEntity.ReplaceHitPoint(TargetEntity.hitPoint.value - e.damage.value);
                    
                    break;
                case "Enemy":
                    
                        TargetEntity = enemyContext.GetEntityWithName(e.targetInformation.Name);
                        TargetEntity.ReplaceHitPoint(TargetEntity.hitPoint.value - e.damage.value);
                    
                    break;
                default:
                    break;
            }
        }
    }

    protected override bool Filter(CollisionEntity entity)
    {
        
        return entity.hasTargetInformation;
    }

    protected override ICollector<CollisionEntity> GetTrigger(IContext<CollisionEntity> context)
    {
        return context.CreateCollector(CollisionMatcher.TargetInformation.Added());
    }
}

